package com.atguigu.servicebase;


import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration//配置类
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket webApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)  // 指定api类型为swagger2
                .groupName("webApi")
                .apiInfo(webApiInfo())                 // 用于定义api文档汇总信息
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.atguigu.controller"))   // 指定controller包
                //.paths(PathSelectors.any())         // 所有controller
                /*.paths(Predicates.not(PathSelectors.regex("/admin/.*")))*/
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build()
                .globalOperationParameters(setHeaderToken());
    }

    private ApiInfo webApiInfo() {
        return new ApiInfoBuilder()
                .title("网站-课程中心API文档")        // 文档页标题
                .contact(new Contact("Helen",
                        "http://atguigu.com",
                        "55317332@qq.com"))     // 联系人信息
                .description("本文档描述了课程中心微服务接口定义")  // 详细信息
                .version("1.0")   // 文档版本号
                .termsOfServiceUrl("http://atguigu.com") // 网站地址
                .build();
    }

    /**
     * token
     *
     * @return
     */
    private List<Parameter> setHeaderToken() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("token").description("token")
                .modelRef(new ModelRef("string"))
                .parameterType("header").required(true)
                .build();
        pars.add(tokenPar.build());
        return pars;
    }
}
